<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='global-property-S-'>/**
</span> * @fileOverview 表格单元格编辑
 * @ignore
 */

var $ = require(&#39;jquery&#39;),
  Editing = require(&#39;./editing&#39;),
  CLS_BODY = BUI.prefix + &#39;grid-body&#39;,
  CLS_CELL = BUI.prefix + &#39;grid-cell&#39;;

<span id='BUI-Grid-Plugins-CellEditing'>/**
</span> * @class BUI.Grid.Plugins.CellEditing
 * @extends BUI.Grid.Plugins.Editing
 * 单元格编辑插件
 */
var CellEditing = function(config){
  CellEditing.superclass.constructor.call(this, config);
};

CellEditing.ATTRS = {
<span id='BUI-Grid-Plugins-CellEditing-cfg-triggerCls'>  /**
</span>   * 触发编辑样式，为空时默认点击整行都会触发编辑
   * @cfg {String} [triggerCls = &#39;bui-grid-cell&#39;]
   */
  triggerCls : {
    value : CLS_CELL
  }
};

BUI.extend(CellEditing,Editing);

BUI.augment(CellEditing,{
<span id='BUI-Grid-Plugins-CellEditing-method-getEditorCfgs'>  /**
</span>   * @protected
   * 获取编辑器的配置项
   * @param  {Array} fields 字段配置
   */ 
  getEditorCfgs : function(fields){
    var _self = this,
      grid = _self.get(&#39;grid&#39;),
      bodyNode = grid.get(&#39;el&#39;).find(&#39;.&#39; + CLS_BODY),
      rst = [];
    BUI.each(fields,function(field){
      var cfg = {field : field,changeSourceEvent : null,hideExceptNode : bodyNode,autoUpdate : false,preventHide : false,editableFn : field.editableFn};
      if(field.xtype === &#39;checkbox&#39;){
        cfg.innerValueField = &#39;checked&#39;;
      }
      rst.push(cfg);
    });

    return rst;
  },
<span id='BUI-Grid-Plugins-CellEditing-method-getEditor'>  /**
</span>   * 获取编辑器
   * @protected
   * @param  {String} field 字段值
   * @return {BUI.Editor.Editor}  编辑器
   */
  getEditor : function(field){
    if(!field){
      return null;
    }
    var  _self = this,
      editors = _self.get(&#39;editors&#39;),
      editor = null;

    BUI.each(editors,function(item){
      if(item.get(&#39;field&#39;).get(&#39;name&#39;) === field){
        editor = item;
        return false;
      }
    });
    return editor;
  },
<span id='BUI-Grid-Plugins-CellEditing-method-beforeShowEditor'>  /**
</span>   * 显示编辑器前
   * @protected
   * @param  {BUI.Editor.Editor} editor 
   * @param  {Object} options
   */
  beforeShowEditor : function(editor,options){
    var _self = this,
      cell = $(options.cell);
    _self.resetWidth(editor,cell.outerWidth());
    _self._makeEnable(editor,options);
  },
  _makeEnable : function(editor,options){
    var editableFn = editor.get(&#39;editableFn&#39;),
      field,
      enable,
      record;
    if(BUI.isFunction(editableFn)){
      field = options.field;
      record = options.record;
      if(record &amp;&amp; field){
        enable = editableFn(record[field],record);
        if(enable){
          editor.get(&#39;field&#39;).enable();
        }else{
          editor.get(&#39;field&#39;).disable();
        }
      }
      
    }
  },
  resetWidth : function(editor,width){
    editor.set(&#39;width&#39;,width);
  },
<span id='BUI-Grid-Plugins-CellEditing-method-updateRecord'>  /**
</span>   * 更新数据
   * @protected
   * @param  {Object} record 编辑的数据
   * @param  {*} value  编辑值
   */
  updateRecord : function(store,record,editor){
    var _self = this,
        value = editor.getValue(),
        fieldName = editor.get(&#39;field&#39;).get(&#39;name&#39;),
        preValue = record[fieldName];
      value = BUI.isDate(value) ? value.getTime() : value;
      if(preValue !== value){
        store.setValue(record,fieldName,value);
      }
  },
<span id='BUI-Grid-Plugins-CellEditing-method-getAlignNode'>  /**
</span>   * @protected
   * 获取对齐的节点
   * @override
   * @param  {Object} options 点击单元格的事件对象
   * @return {jQuery} 
   */
  getAlignNode : function(options){
    return $(options.cell);
  },
<span id='BUI-Grid-Plugins-CellEditing-method-getFields'>  /**
</span>   * 获取编辑的字段
   * @protected
   * @return {Array}  字段集合
   */
  getFields : function(){
    var rst = [],
      _self = this,
      editors = _self.get(&#39;editors&#39;);
    BUI.each(editors,function(editor){
      rst.push(editor.get(&#39;field&#39;));
    });
    return rst;
  },
<span id='BUI-Grid-Plugins-CellEditing-method-getEditValue'>  /**
</span>   * @protected
   * 获取要编辑的值
   * @param  {Object} options 点击单元格的事件对象
   * @return {*}   编辑的值
   */
  getEditValue : function(options){
    if(options.record &amp;&amp; options.field){
      var value = options.record[options.field];
      return value == null ? &#39;&#39; : value;
    }
    return &#39;&#39;;
  }
});

module.exports = CellEditing;
</pre>
</body>
</html>
